package com.example.Servlet;

import com.example.Function.CutSentence;
import com.example.Function.DataBaseOp;
import com.example.Object.Question;
import com.google.gson.Gson;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author echo
 * 自动接收题目
 */

@WebServlet("/ReceiveFile")

public class ReceiveFile extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //工厂
        DiskFileItemFactory factory = new DiskFileItemFactory();
        //设置缓冲区大小 和 零食文件目录
        factory.setSizeThreshold(1024 * 1024 * 10);
        ServletFileUpload upload = new ServletFileUpload(factory);
        File uploadTemp = new File("C:\\temp\\");
        uploadTemp.mkdirs();
        factory.setRepository(uploadTemp);
        String uuid = null;
        String suffix = null;
        //设置文件大小限制
        upload.setFileSizeMax(1024 * 1024 * 10);
        //设置文件综合大小限制
        upload.setSizeMax(1024 * 1024 * 100);
        upload.setHeaderEncoding("UTF-8");

        List<FileItem> list = new ArrayList<>();
        try {
            list = upload.parseRequest(req);
            for (FileItem fileItem : list) {
                String fileName = fileItem.getName();
                uuid = UUID.randomUUID().toString();
                suffix = fileName.substring(fileName.lastIndexOf("."));

                InputStream is=fileItem.getInputStream();
                BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\temp\\"+uuid+suffix));
                bw.write(fileItem.getString("utf-8"));
                bw.flush();


                is.close();
                bw.close();

                fileItem.delete();
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        //设置要裁剪的题目
        CutSentence.setOrg(uuid+suffix);
        //裁剪
        ArrayList<Question> questions = CutSentence.cutQuestion();


        if(DataBaseOp.publicTile != null){
            //裁剪成功，发送到数据库
            DataBaseOp.insertQuestion(questions,DataBaseOp.publicTile);
            System.out.println("成功");
        }else{
            System.out.println("失败");
            return;
        }


        PrintWriter out = resp.getWriter();
        Gson gson = new Gson();
        out.println(gson.toJson(true));


    }



    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
